<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
require_once 'model/model.php';

?>

<h1>Van Persie</h1>
<?php 
    $query = "SELECT * FROM companies";
    $companies = getData($query);?>
<form method="POST">
    Min fans<input type="text" name="min_fans"><br>
    Max fans<input type="text" name="max_fans"><br>
    Keyword <input type="text" name="keyword"><br>
    Company :<select name="id_company">
        <option value='0'>----Company----</option>
        <?php foreach ($companies as $company): ?>
            <option value="<?php echo $company['id'] ?>">
                       <?php echo $company['name']?></option>
        <?php endforeach;?>
    </select><br>
    App     :<select name="id_app">
        <?php $query = "SELECT * FROM apps";
    $AS = getData($query);?>
        <option value='0'>-------App-------</option>
        <?php foreach ($AS as $aps): ?>
            <option value="<?php echo $aps['id'] ?>">
                       <?php echo $aps['name']?></option>
        <?php endforeach;?>
    </select>
    <input type="submit" name="submit">
</form>

<?php if (isset($_POST['submit'])):?>
    <?php 
        $min_fans = $_POST['min_fans'];
        $max_fans = $_POST['max_fans'];  
        $keyword = $_POST['keyword'];
        $compa=$_POST['id_company'];
        $appps=$_POST['id_app'];
        if($compa==0 && $appps==0){
            if(empty($keyword)){
                if(empty($max_fans)&&empty($min_fans)){
                     $query = "SELECT * FROM pages
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if(empty ($max_fans)){
                     $query = "SELECT * FROM pages 
                      WHERE (likes >= $min_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if (empty ($min_fans)){
                     $query = "SELECT * FROM pages 
                      WHERE (likes <= $max_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else{
                     $query = "SELECT * FROM pages 
                      WHERE (likes <= $max_fans)
                      AND (likes >= $min_fans)
                      LIMIT 0,10
                      ";
                      $pages = getData($query);
                }
            }else{
                if(empty($max_fans)&&empty($min_fans)){
                     $query = "SELECT * FROM pages WHERE
                      (description LIKE '% $keyword %')
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if(empty ($max_fans)){
                     $query = "SELECT * FROM pages WHERE
                      (description LIKE '% $keyword %')
                      AND (likes >= $min_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if(empty ($min_fans)){
                     $query = "SELECT * FROM pages WHERE
                      (description LIKE '% $keyword %')
                      AND (likes <= $max_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else{
                     $query = "SELECT * FROM pages WHERE
                      (description LIKE '% $keyword %')
                      AND (likes <= $max_fans)
                      AND (likes >= $min_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }
            }
        }else if(($compa !=0)&&($appps==0)){
                if(empty($keyword)){
                if(empty($max_fans)&&empty($min_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if(empty ($max_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id    
                      WHERE (likes >= $min_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if (empty ($min_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE (likes <= $max_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else{
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE (likes <= $max_fans)
                      AND (likes >= $min_fans)
                      LIMIT 0,10
                      ";
                      $pages = getData($query);
                }
            }else{
                if(empty($max_fans)&&empty($min_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE(description LIKE '% $keyword %')
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if(empty ($max_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE(description LIKE '% $keyword %')
                      AND (likes >= $min_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if(empty ($min_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE(description LIKE '% $keyword %')
                      AND (likes <= $max_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else{
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE(description LIKE '% $keyword %')
                      AND (likes <= $max_fans)
                      AND (likes >= $min_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }
            }
        }else if(($compa ==0)&&($appps!=0)){
                if(empty($keyword)){
                if(empty($max_fans)&&empty($min_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if(empty ($max_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id    
                      WHERE (likes >= $min_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if (empty ($min_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE (likes <= $max_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else{
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE (likes <= $max_fans)
                      AND (likes >= $min_fans)
                      LIMIT 0,10
                      ";
                      $pages = getData($query);
                }
            }else{
                if(empty($max_fans)&&empty($min_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE(description LIKE '% $keyword %')
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if(empty ($max_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE(description LIKE '% $keyword %')
                      AND (likes >= $min_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if(empty ($min_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE(description LIKE '% $keyword %')
                      AND (likes <= $max_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else{
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE(description LIKE '% $keyword %')
                      AND (likes <= $max_fans)
                      AND (likes >= $min_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }
            }
                
            }else{
                     if(empty($keyword)){
                if(empty($max_fans)&&empty($min_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if(empty ($max_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id    
                      WHERE (likes >= $min_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if (empty ($min_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE (likes <= $max_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else{
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE (likes <= $max_fans)
                      AND (likes >= $min_fans)
                      LIMIT 0,10
                      ";
                      $pages = getData($query);
                }
            }else{
                if(empty($max_fans)&&empty($min_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE(description LIKE '% $keyword %')
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if(empty ($max_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE(description LIKE '% $keyword %')
                      AND (likes >= $min_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else if(empty ($min_fans)){
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE(description LIKE '% $keyword %')
                      AND (likes <= $max_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }else{
                     $query = "SELECT * FROM pages
                      INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                      INNER JOIN apps ON apps_pages.id_app=apps.id
                      INNER JOIN companies ON apps.id_company=companies.id
                      WHERE(description LIKE '% $keyword %')
                      AND (likes <= $max_fans)
                      AND (likes >= $min_fans)
                      LIMIT 0, 10
                      ";
                      $pages = getData($query);
                }
            }
            }
        var_dump($query);
    ?>

    <?php if (!empty($pages)):?>
    <table border="1">
        <tr>
            <td> profile </td>
            <td> likes </td>
            <td> description</td>
            <td> name</td>
        </tr>
        <?php foreach ($pages as $page):?>
            <tr>
                <td><img src="<?php echo $page['picture']?>"></td>
                <td><?php echo $page['likes'] ?></td>
                <td><?php echo substr($page['description'],0,100); ?></td>
                <td><?php echo $page['name']; ?></td>
                <?php // var_dump($page);die();?>
            </tr>
        <?php endforeach; ?>
    </table> 
    <?php endif;?>
<?php endif;?>